package array

import (
	"fmt"
)

type LoopQueue interface {
	GetSize()int
	IsEmpty()bool
	IsFull()bool
	EnQueue(v interface{}) error
	DeQueue()(interface{},error)
}

type FlLoopQueue struct {
	fl *FlArray
	size int
	tail int
	head int
}

func NewFlLoopQueue(size int) LoopQueue {
	return &FlLoopQueue{
		fl: New(WithCapacity(size+1),WithSize(size+1)),
		size: size+1,
		tail: 0,
		head: 0,
	}
}

func (f *FlLoopQueue) GetSize() int {
	return f.size
}

func (f *FlLoopQueue) IsEmpty() bool {
	return f.head==f.tail
}

func (f *FlLoopQueue) IsFull() bool {
	return (f.tail+1)%f.size==f.head
}

func (f *FlLoopQueue) EnQueue(v interface{}) error {
	if f.IsFull() {
		return fmt.Errorf("queue is full")
	}
	f.fl.Update(f.tail,v)
	f.tail = (f.tail+1)%f.size
	return nil
}

func (f *FlLoopQueue) DeQueue()(interface{},error)  {
	if f.IsEmpty() {
		return nil,fmt.Errorf("queue is empty")
	}
	v := f.fl.Find(f.head)
	f.head = (f.head+1)%f.size
	return v,nil
}